//
//  DDDonationVC.m
//  dingdang
//
//  Created by 阮 沧晖 on 15/7/18.
//  Copyright (c) 2015年 阮 沧晖. All rights reserved.
//

#import "DDDonationVC.h"
#import "DDNetWorkAPIClient.h"
#import "DDForgetPwdVC.h"
@interface DDDonationVC ()

@property (weak, nonatomic) IBOutlet UITextField *fenzhiTF;
@property (weak, nonatomic) IBOutlet UITextField *donationDDTF;
@property (weak, nonatomic) IBOutlet UITextField *sendPasswrd;
@end

@implementation DDDonationVC

- (void)viewDidLoad {
    NSLog(@"进入到---------->DDDonationVC");
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self createUI];
}

- (void)createUI
{
    self.view.backgroundColor = RGB(245, 245, 245);
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBarHidden = YES;
    //自定义TAB BAR UI
    [self addNavigatorTitle:NSLocalizedString(@"转赠积分", nil) parent:self.view];
    self.barViewControl.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"top_Background"]];
    [self AddLeftBtnAction:nil normal:@"top_返回" selected:@"top_返回" action:^{
        //[self dismissViewControllerAnimated:YES completion:nil];
        [self.navigationController popViewControllerAnimated:YES];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)submitDonationAction:(id)sender {
    if (_fenzhiTF.text.length < 1) {
        [self delayHUD:@"请输入分值"];
        return;
    }
    if (_donationDDTF.text.length < 1) {
        [self delayHUD:@"请输入转赠的账号"];
        return;
    }
    
    if (_sendPasswrd.text.length < 1) {
        [self delayHUD:@"请输入支付密码"];
        return;
    }
    [_confirmBtn setEnabled:NO];
    [self showHUD:@"赠予中"];
    DDNetWorkAPIClient *network = [[DDNetWorkAPIClient alloc] init];
    [network gitGave:@{@"doneeNo":_donationDDTF.text,@"giveNum":_fenzhiTF.text,@"payPassword":_sendPasswrd.text} completionBlock:^(BOOL isSuccess, NSDictionary *dict, NSString *message, NSString *status) {
        [self hideHUD];
        if (isSuccess) {
            [self delayHUDWithPopVC:@"赠予成功"];
        }else{
            [self delayHUD:@"赠予失败"];
            [_confirmBtn setEnabled:YES];
        }
    }];
}

- (IBAction)payTxtEditingBegin:(id)sender {
    DDSharedUser *user = [DDSharedUser shared];
    DDNetWorkAPIClient *network = [[DDNetWorkAPIClient alloc]init];
    [network getUserInfo:user.access_token completionBlock:^(BOOL isSuccess, NSDictionary *dict, NSString *message, NSString *status) {
        DDSharedUser *sharedUser = [DDSharedUser shared];
        BOOL hasPayPassword = [sharedUser.userInfo.hasPayPassword intValue] != 0;
        if (isSuccess) {
            hasPayPassword = [[dict objectForKey:@"hasPayPassword"] boolValue];
            sharedUser.userInfo.hasPayPassword = [dict objectForKey:@"hasPayPassword"];
        }
        
        if (!hasPayPassword) {// 没有支付密码 需要设置支付密码
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您还没有设置支付密码,是否设置支付密码！" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"去设置", nil];
            [alertView show];
            [self.sendPasswrd resignFirstResponder];
        }
    }];
}

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 1) {
        [self setPasswrdAction:nil];
    }
}

- (IBAction)setPasswrdAction:(id)sender {
    DDForgetPwdVC *fogetPwd = [[DDForgetPwdVC alloc] initWithNibName:@"DDForgetPwdVC" bundle:nil];
    [self.navigationController pushViewController:fogetPwd animated:YES];
}

@end
